package com.zwps.biz.controller.http;

import com.alibaba.fastjson.JSONObject;
import com.zwps.biz.domain.service.CasService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Tag(name = "cas单点登录")
@RequestMapping("cas")
public class CasController {

    @Autowired
    private  CasService casService;

    @Value("${application.cas.redirectUrl}")
    private  String redirectUrl;

    @Operation(summary = "获取用户信息")
    @GetMapping("getUserByToken")
    public JSONObject getUserByToken(String cToken) {
        return casService.getUserByToken(cToken);
    }

    @Operation(summary = "校验token信息")
    @PostMapping("validateToken")
    public String validateToken(String cToken) {
        return casService.validateToken(cToken);
    }

    @Operation(summary = "cas登录")
    @GetMapping("login")
    public void login(String ticket, HttpServletRequest request, HttpServletResponse response) {
        String cToken = (String) request.getAttribute("cToken");
        casService.login(ticket, cToken);
        response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
        response.setHeader("Location", redirectUrl + cToken);
    }
}
